<?php
include_once(PATH_SQL . 'packet_sql.php');
include_once(PATH_MODEL . 'packet_model.php');
include_once(PATH_VIEW_ADMIN . 'packet_view.php');
include_once(PATH_INCLUDE."constant.php");
include_once(PATH_INCLUDE."thumb.php");

$oModelPacket = new Model_Packet();
$oViewPacket = new View_Admin_Packet($oModelPacket);
$sAction=$_REQUEST['sAction'];
$oUpload = new Upload($_FILES);
switch($sAction) {
    case "add_new_form_packet": 
    	$oViewPacket->addNewPacketForm($oModelPacket);
    	break;
    case "do_add_new":
    	$fileUpload = "";
		if ($oUpload->getFileName("packet_image") != "") 	{
				if ($oUpload->isAllowImageFile("packet_image")) {
//					die(FOLDER_UPLOAD_PACKET);
                        $ext = $oUpload->getFileExtension("packet_image");
                        $fileUpload = time().$oUpload->getFileName("packet_image");
                        if ($oUpload->saveAs($fileUpload, PATH_UPLOAD_PACKET 	,"packet_image", "over", 0777)) 
						{
								
						$thumb=new thumbnail_1(PATH_UPLOAD_PACKET.$fileUpload);	// generate image_file, set filename to resize
						/**
						 * Set thumb 300x200
						 */
						$thumb->size_width(THUMBNAIL_IMAGE_SIZE_WIDTH_300);
						$thumb->size_height(THUMBNAIL_IMAGE_SIZE_HEIGHT_200);
						$thumb->size_auto(THUMBNAIL_IMAGE_SIZE_AUTO_300);
						$thumb->jpeg_quality(THUMBNAIL_IMAGE_SIZE_QUALITY);
						$thumb->save(PATH_UPLOAD_PACKET."thumb300x200/".$fileUpload);
						/**
						 * Set thumb 160x120
						 */
						/*$thumb->size_width(THUMBNAIL_IMAGE_SIZE_WIDTH);
						$thumb->size_height(THUMBNAIL_IMAGE_SIZE_HEIGHT);
						$thumb->size_auto(THUMBNAIL_IMAGE_SIZE_AUTO);
						$thumb->jpeg_quality(THUMBNAIL_IMAGE_SIZE_QUALITY);
						$thumb->save(PATH_UPLOAD_PACKET."thumbnails/".$fileUpload);*/
                        }
                    }
                }
    	$oModelPacket->insertPacket($fileUpload);
    	redir('?sMod=packet_index');
    	break;
    case"do_delete":
    	$oModelPacket->deletePacketById($_REQUEST[id]);
    	redir('?sMod=packet_index');
    	break;
    case"edit_form":
    	$oViewPacket->editPacketForm($oModelPacket);
    	break;
    case"do_edit":
    	$fileUpload = "";
			if ($oUpload->getFileName("packet_image") != "") 	{
				if ($oUpload->isAllowImageFile("packet_image")) {
//					die(FOLDER_UPLOAD_PACKET);
                        $ext = $oUpload->getFileExtension("packet_image");
                        $fileUpload = time().$oUpload->getFileName("packet_image");
                        if ($oUpload->saveAs($fileUpload, PATH_UPLOAD_PACKET 	,"packet_image", "over", 0777)) 
						{
								
						$thumb=new thumbnail_1(PATH_UPLOAD_PACKET.$fileUpload);	// generate image_file, set filename to resize
						/**
						 * Set thumb 300x200
						 */
						$thumb->size_width(THUMBNAIL_IMAGE_BACKET_SIZE_WIDTH_300);
						$thumb->size_height(THUMBNAIL_IMAGE_BACKET_SIZE_HEIGHT_200);
						$thumb->size_auto(THUMBNAIL_IMAGE_BACKET_SIZE_AUTO_300);
						$thumb->jpeg_quality(THUMBNAIL_IMAGE_BACKET_SIZE_QUALITY);
						$thumb->save(PATH_UPLOAD_PACKET."thumb300x200/".$fileUpload);
						/**
						 * Set thumb 160x120
						 */
						/*$thumb->size_width(THUMBNAIL_IMAGE_SIZE_WIDTH);
						$thumb->size_height(THUMBNAIL_IMAGE_SIZE_HEIGHT);
						$thumb->size_auto(THUMBNAIL_IMAGE_SIZE_AUTO);
						$thumb->jpeg_quality(THUMBNAIL_IMAGE_SIZE_QUALITY);
						$thumb->save(PATH_UPLOAD_PACKET."thumbnails/".$fileUpload);*/
                        }
                    }
                }else{
                	$fileUpload = $_REQUEST[packet_image_old];
                }
        $oModelPacket->updatePacketById($fileUpload, $_REQUEST[id]);
    	redir('?sMod=packet_index');
    case "add_new_form_packet": 
    	$oViewPacket->addNewPacketForm($oModelPacket);
    	break;
    case "do_add_new":
    	$fileUpload = "";
		if ($oUpload->getFileName("packet_image") != "") 	{
				if ($oUpload->isAllowImageFile("packet_image")) {
//					die(FOLDER_UPLOAD_PACKET);
                        $ext = $oUpload->getFileExtension("packet_image");
                        $fileUpload = time().$oUpload->getFileName("packet_image");
                        if ($oUpload->saveAs($fileUpload, PATH_UPLOAD_PACKET 	,"packet_image", "over", 0777)) 
						{
								
						$thumb=new thumbnail_1(PATH_UPLOAD_PACKET.$fileUpload);	// generate image_file, set filename to resize
						/**
						 * Set thumb 300x200
						 */
						$thumb->size_width(THUMBNAIL_IMAGE_SIZE_WIDTH_300);
						$thumb->size_height(THUMBNAIL_IMAGE_SIZE_HEIGHT_200);
						$thumb->size_auto(THUMBNAIL_IMAGE_SIZE_AUTO_300);
						$thumb->jpeg_quality(THUMBNAIL_IMAGE_SIZE_QUALITY);
						$thumb->save(PATH_UPLOAD_PACKET."thumb300x200/".$fileUpload);
						/**
						 * Set thumb 160x120
						 */
						/*$thumb->size_width(THUMBNAIL_IMAGE_SIZE_WIDTH);
						$thumb->size_height(THUMBNAIL_IMAGE_SIZE_HEIGHT);
						$thumb->size_auto(THUMBNAIL_IMAGE_SIZE_AUTO);
						$thumb->jpeg_quality(THUMBNAIL_IMAGE_SIZE_QUALITY);
						$thumb->save(PATH_UPLOAD_PACKET."thumbnails/".$fileUpload);*/
                        }
                    }
                }
    	$oModelPacket->insertPacket($fileUpload);
    	redir('?sMod=packet_index');
    case"do_delete":
    	$oModelPacket->deletePacketById($_REQUEST[id]);
    	redir('?sMod=packet_index');
    	break;
    case "buy_packet":
        $packet = $oModelPacket->getPacketById($_REQUEST[id]);
        $trans= array();
        if(count($packet->fields)>0 ){
            $oPacket = $packet->fields;
            $trans["user_id"] = $_SESSION['admin_id'];
            $trans["packet_id"] = $oPacket['id'];
            $trans["download_items"] = $oPacket['download_items'];
            $trans["periods"] = $oPacket['period'];
            $trans['price'] = $oPacket['price'];
            $trans['status'] = TRANS_PENDING;
            $trans['activity'] = date("Y-m-d H:i:s");
            $oModelPacket->doInsertTrans($trans);
        }
    	redir('?sMod=packet_index&Action=buy_success');
    case"edit_form":
    	$oViewPacket->editPacketForm($oModelPacket);
    	break;
    case"do_edit":
    	$fileUpload = "";
			if ($oUpload->getFileName("packet_image") != "") 	{
				if ($oUpload->isAllowImageFile("packet_image")) {
//					die(FOLDER_UPLOAD_PACKET);
                        $ext = $oUpload->getFileExtension("packet_image");
                        $fileUpload = time().$oUpload->getFileName("packet_image");
                        if ($oUpload->saveAs($fileUpload, PATH_UPLOAD_PACKET 	,"packet_image", "over", 0777)) 
						{
								
						$thumb=new thumbnail_1(PATH_UPLOAD_PACKET.$fileUpload);	// generate image_file, set filename to resize
						/**
						 * Set thumb 300x200
						 */
						$thumb->size_width(THUMBNAIL_IMAGE_SIZE_WIDTH_300);
						$thumb->size_height(THUMBNAIL_IMAGE_SIZE_HEIGHT_200);
						$thumb->size_auto(THUMBNAIL_IMAGE_SIZE_AUTO_300);
						$thumb->jpeg_quality(THUMBNAIL_IMAGE_SIZE_QUALITY);
						$thumb->save(PATH_UPLOAD_PACKET."thumb300x200/".$fileUpload);
						/**
						 * Set thumb 160x120
						 */
						/*$thumb->size_width(THUMBNAIL_IMAGE_SIZE_WIDTH);
						$thumb->size_height(THUMBNAIL_IMAGE_SIZE_HEIGHT);
						$thumb->size_auto(THUMBNAIL_IMAGE_SIZE_AUTO);
						$thumb->jpeg_quality(THUMBNAIL_IMAGE_SIZE_QUALITY);
						$thumb->save(PATH_UPLOAD_PACKET."thumbnails/".$fileUpload);*/
                        }
                    }
                }else{
                	$fileUpload = $_REQUEST[packet_image_old];
                }
        $oModelPacket->updatePacketById($fileUpload, $_REQUEST[id]);
    	redir('?sMod=packet_index');
    default:
		$oViewPacket->displayPacketIndex($oModelPacket);
		break;
}

$oViewPacket->displayPage($oModelPacket);
